/** 重启服务 */
const fs = require("fs");
const PATH = require("path");
const path = require("path");

// main("C:\\Users\\tangx\\Downloads\\m3u8dl\\test\\");

async function main(workDir) {
  let readDir = fs.readdirSync(workDir);

  for (const folder of readDir) {
    console.log("TT > folder", folder);
    const fileDir = PATH.join(workDir, folder);
    const isFolder = await isDir(fileDir);
    if (!isFolder) continue;

    let obj = readFile(fileDir);
    const m3u8Str = obj.m3u8.split('?')[0] + "t=1662731441&m=tXGTVyB1VojTfaR0_O2cNQ";
    console.log("TT > m3u8Str", m3u8Str);
    const url = combineUrl(m3u8Str, folder);
    console.log("TT > url", url);
  }
}
function readFile(filePath) {
  let rawdata = fs.readFileSync(`${filePath}/meta.json`);
  let json = JSON.parse(rawdata);
  return json;
}

function isDir(fileDir) {
  return new Promise((resolve, reject) => {
    fs.stat(fileDir, function (err, stats) {
      if (stats.isDirectory()) resolve(true);
      resolve(false);
    });
  });
}
function combineUrl(m3u8Url, fileName) {
  var str =
    '"' +
    m3u8Url +
    '" --workDir "%USERPROFILE%\\Downloads\\m3u8dl" --saveName "' +
    fileName +
    '" --enableDelAfterDone --disableDateInfo --noProxy';

  // create a buffer
  const buff = Buffer.from(str, "utf-8");
  // encode buffer as Base64
  const base64 = buff.toString("base64");

  return "m3u8dl://" + base64;
  // return "m3u8dl://" + str;
}
// 1662726438&m=PV8cizjhwwC6YbQR-AdH2g

